﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Routing;

namespace SelfManagement.Mvc.Helper
{
    public static class RouteValuesHelper
    {
        public static RouteValueDictionary FromNVC(NameValueCollection nv)
        {
            var routeValues = new RouteValueDictionary();

            foreach (var key in nv.AllKeys)
            {
                if (string.IsNullOrEmpty(key))
                    continue;
                routeValues.Add(key, nv[key]);
            }

            return routeValues;
        }

        public static RouteValueDictionary From(object obj)
        {
            var routeValues = new RouteValueDictionary();

            foreach (var property in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                try
                {
                    routeValues.Add(property.Name, property.GetValue(obj));
                }
                catch
                {
                }
            }

            return routeValues;
        }
    }
}